<?php
namespace App\Http\Controllers\Supplier;

use App\Models\Goods;
use App\Models\ShopBanner;
use App\Models\ShopCategory;
use App\Models\ShopGoods;
use App\Services\CommonService;
use Illuminate\Http\Request;

//旺铺管理
class HotShopController extends BaseController
{
    //商品排序列表
    public function goodsSortList(Request $request)
    {
        $input = $request->input();
        $where = [];
        $where[] = ['g.member_id', $this->shopUser['member_id']];
        $where[] = ['g.status', Goods::GOODS_STATUS_UP];
        $where[] = ['g.is_delete', Goods::GOODS_NOT_DELETE];
        if (!empty($input['title'])) {
            $where[] = ['g.title', 'like', '%' . trim($input['title']) . '%'];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $select = array('g.id', 'g.member_id', 'g.shop_sort', 'g.imgurl', 'g.title', 'g.create_time', 'g.status', 'c.name');
        $data = Goods::from('sp_goods_2 as g')
            ->leftjoin('sp_category as c', 'c.id', 'g.category_id')
            ->select($select)
            ->where($where)
            ->orderBy('g.shop_sort', 'asc')
            ->orderBy('g.create_time', 'asc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['create_time'] = !empty($v['create_time']) ? dateFormat($v['create_time']) : '';
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id编辑(商品/店铺分类/店铺关联商品)的排序
    public function editSort(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        if (!is_numeric($input['sort'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '排序只能是数字');
        }
        if ($input['type'] == 'Goods') { //编辑商品的排序
            Goods::editDataById($input['id'], array('shop_sort' => $input['sort']));
        } elseif ($input['type'] == 'ShopCategory') { //编辑店铺分类的排序
            ShopCategory::editDataById($input['id'], array('sort' => $input['sort']));
        } elseif ($input['type'] == 'ShopGoods') { //编辑店铺关联商品的排序
            ShopGoods::editDataById($input['id'], array('sort' => $input['sort']));
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '编辑排序类型错误,请核实');
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }


    //店铺轮播图设置列表
    public function shopBannerList(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $shopBanner = ShopBanner::GetShopBannerList($pageSize, array('sb.member_id' => $this->shopUser['member_id'], 'sb.is_delete' => ShopBanner::SHOP_BANNER_NOT_DELETE));
        $shopBanner = formatPage($shopBanner);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $shopBanner);
    }

    //根据id查询店铺轮播图
    public function shopBannerById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $data = ShopBanner::getShopBannerById($input['id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取商品信息(公用)
    public function getGoods(Request $request)
    {
        $input = $request->input();
        $where = [];
        $orwhere = [];
        $where[] = ['g.member_id', $this->shopUser['member_id']];
        $where[] = ['g.status', Goods::GOODS_STATUS_UP];
        $where[] = ['g.is_delete', Goods::GOODS_NOT_DELETE];
        if (!empty($input['title'])) {
            $where[] = ['g.title', 'like', '%' . trim($input['title']) . '%'];
            $orwhere[] = ['g.id', trim($input['title'])];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = Goods::getGoods($pageSize, $where, $orwhere);
        $data = formatPage($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加或编辑店铺轮播图
    public function addOrEdiShopBanner(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) { //添加
            $addData = array(
                'member_id' => $this->shopUser['member_id'],
                'banner_img' => $input['banner_img'],
                'goods_id' => $input['goods_id'],
                'sort' => 1,
                'create_time' => date('Y-m-d H:i:s'),
            );
            $res = ShopBanner::addData($addData);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            ShopBanner::editDataById($input['id'], $input);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        }
    }

    //根据id删除店铺轮播图
    public function delShopBanner(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        ShopBanner::editDataById($input['id'], array('is_delete' => ShopBanner::SHOP_BANNER_IS_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }


    //店铺分类列表
    public function shopCategoryList(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $select = array('id', 'member_id', 'sort', 'cat_name', 'create_time');
        $shopCategory = ShopCategory::GetShopCategoryList($pageSize, array('member_id' => $this->shopUser['member_id'], 'is_delete' => ShopCategory::SHOP_CATEGORY_DELETE_NO));
        $shopCategory = formatPage($shopCategory);
        $info = $shopCategory['data'];
        foreach ($info as $k => $v) {
            $info[$k]['goodsCount'] = ShopGoods::where(array('member_id' => $this->shopUser['member_id'], 'cat_id' => $v['id'], 'is_delete' => ShopGoods::SHOP_GOODS_NOT_DELETE))->count();
        }
        $shopCategory['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $shopCategory);
    }

    //添加店铺分类
    public function addShopCategory(Request $request)
    {
        $input = $request->input();
        if (empty($input['cat_name'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入分类名称');
        }
        $shopCategory = ShopCategory::where(array('member_id' => $this->shopUser['member_id'], 'cat_name' => $input['cat_name']))->first();
        if (empty($shopCategory)) { //添加
            $addData = array(
                'cat_name' => $input['cat_name'],
                'member_id' => $this->shopUser['member_id'],
                'sort' => 1,
                'create_time' => date('Y-m-d H:i:s'),
            );
            $res = ShopCategory::addData($addData);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            if ($shopCategory['is_delete'] == ShopCategory::SHOP_CATEGORY_DELETE_NO) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该分类已存在,请勿重复添加');
            } else {
                ShopCategory::editDataById($shopCategory['id'], array('is_delete' => ShopCategory::SHOP_CATEGORY_DELETE_NO,'create_time' => date('Y-m-d H:i:s')));
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            }
        }

    }

    //根据id查询店铺分类
    public function shopCategoryById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $data = ShopCategory::GetShopCategoryById($input['id']);
        $data['goodsCount'] = ShopGoods::where(array('member_id' => $this->shopUser['member_id'], 'cat_id' => $data['id'], 'is_delete' => ShopGoods::SHOP_GOODS_NOT_DELETE))->count();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id删除店铺分类
    public function delShopCategory(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        ShopCategory::editDataById($input['id'], array('is_delete' => ShopCategory::SHOP_CATEGORY_DELETE_YES));
        ShopGoods::where(array('member_id' => $this->shopUser['member_id'], 'cat_id' => $input['id']))->update(array('is_delete' => ShopGoods::SHOP_GOODS_IS_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }

    //店铺关联商品列表
    public function shopGoodsList(Request $request)
    {
        $input = $request->input();
        $where = [];
        $where[] = ['sg.member_id', $this->shopUser['member_id']];
        $where[] = ['sg.cat_id', $input['id']];
        $where[] = ['sg.is_delete', ShopGoods::SHOP_GOODS_NOT_DELETE];
        if (!empty($input['title'])) {
            $where[] = ['g.title', 'like', '%' . trim($input['title']) . '%'];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = ShopGoods::GetShopGoodsList($pageSize, $where);
        $data = formatPage($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加店铺关联商品
    public function addShopGoods(Request $request)
    {
        $input = $request->input();
        if (empty($input['cat_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '分类id为空');
        }
        if (empty($input['goodsidStr'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择商品');
        }
        $goodsidArr = explode(',', $input['goodsidStr']);
        $shopGoods = ShopGoods::whereIn('goods_id', $goodsidArr)->where(array('member_id' => $this->shopUser['member_id'], 'cat_id' => $input['cat_id'], 'is_delete' => ShopGoods::SHOP_GOODS_NOT_DELETE))->get();
        $shopGoods = $shopGoods ? $shopGoods->toArray() : [];
        if (!empty($shopGoods)) {
            $neq2IdStr = '';
            foreach ($shopGoods as $k => $v) {
                $neq2IdStr .= $v['goods_id'] . ',';
            }
            $neq2IdStr = rtrim($neq2IdStr, ',');
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'ID为' . $neq2IdStr . '的商品已存在,请勿重复添加');
        }
        $addData = [];
        foreach ($goodsidArr as $k => $v) {
            $getOneShopGoods = ShopGoods::where(array('member_id' => $this->shopUser['member_id'], 'cat_id' => $input['cat_id'], 'goods_id' => $v))->first();
            $getOneShopGoods = $getOneShopGoods ? $getOneShopGoods->toArray() : array();
            if (!empty($getOneShopGoods)) { //编辑
                ShopGoods::editDataById($getOneShopGoods['id'], array('is_delete' => ShopGoods::SHOP_GOODS_NOT_DELETE,'create_time' => date('Y-m-d H:i:s')));
            } else { //添加
                $addData[$k]['goods_id'] = $v;
                $addData[$k]['cat_id'] = $input['cat_id'];
                $addData[$k]['member_id'] = $this->shopUser['member_id'];
                $addData[$k]['sort'] = 1;
                $addData[$k]['create_time'] = date('Y-m-d H:i:s');
            }
        }
        $res = ShopGoods::addData($addData);
        if ($res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
        }

    }

    //根据id删除店铺关联商品
    public function delShopGoods(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        ShopGoods::editDataById($input['id'], array('is_delete' => ShopGoods::SHOP_GOODS_IS_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }


}
